package com.tudouji.project.produce.controller;

import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.bill.domain.Whoutform;
import com.tudouji.project.produce.domain.Pickinglist;
import com.tudouji.project.produce.service.IPickinglistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 领料单Controller
 *
 * @author tudouji
 * @date 2022-06-01
 */
@RestController
@RequestMapping("/produce/pickinglist")
public class PickinglistController extends BaseController
{
    @Autowired
    private IPickinglistService pickinglistService;

    /**
     * 查询领料单列表
     */
    @GetMapping("/wxlist")
    public TableDataInfo wxlist(Pickinglist pickinglist, String status, String functionType)
    {
        startPage();
        List<Pickinglist> list = pickinglistService.selectPickinglistList(pickinglist, status, functionType);
        return getDataTable(list);
    }

    /**
     * 查询领料单列表
     */
    @PreAuthorize("@ss.hasPermi('produce:pickinglist:list')")
    @GetMapping("/list")
    public TableDataInfo list(Pickinglist pickinglist)
    {
        startPage();
        List<Pickinglist> list = pickinglistService.selectPickinglistListPc(pickinglist);
        return getDataTable(list);
    }

    /**
     * 导出领料单列表
     */
    @PreAuthorize("@ss.hasPermi('produce:pickinglist:export')")
    @Log(title = "领料单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Pickinglist pickinglist)
    {
        List<Pickinglist> list = pickinglistService.selectPickinglistList(pickinglist);
        ExcelUtil<Pickinglist> util = new ExcelUtil<Pickinglist>(Pickinglist.class);
        return util.exportExcel(list, "pickinglist");
    }

    /**
     * 获取领料单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return pickinglistService.getInfo(id);
    }

    /**
     * 新增领料单
     */
    @PreAuthorize("@ss.hasPermi('produce:pickinglist:add')")
    @Log(title = "领料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Pickinglist pickinglist)
    {
        return toAjax(pickinglistService.insertPickinglist(pickinglist));
    }

    /**
     * 修改领料单
     */
    @PreAuthorize("@ss.hasPermi('produce:pickinglist:edit')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pickinglist pickinglist)
    {
        return toAjax(pickinglistService.updatePickinglist(pickinglist));
    }

    /**
     * 删除领料单
     */
    @PreAuthorize("@ss.hasPermi('produce:pickinglist:remove')")
    @Log(title = "领料单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(pickinglistService.deletePickinglistByIds(guids));
    }

    /**
     * 获取领料单详细信息
     */
    @GetMapping(value = "getBaseInfo/{planid}")
    public AjaxResult getBaseInfo(@PathVariable("planid") String planid)
    {
        return pickinglistService.getBaseInfo(planid);
    }

    /**
     * 领料出库操作
     * @param taskid       出库流程任务ID
     * @param entity       出库信息
     * @param id           领料单Id
     * @param opinion      审批意见
     * @param fileIds      上传附件
     * @param opttype      出库类型，启用，后台写死领料出库
     * @return
     */
    @PostMapping("/whout")
    public AjaxResult whout(Long taskid, @RequestBody Whoutform entity, Long id, String opinion, String fileIds, String opttype) {
        return pickinglistService.whout(id, entity, fileIds, opinion, taskid, opttype);
    }

    /**
     * 发起
     */
    @RepeatSubmit
    @Log(title = "发起", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Pickinglist pickinglist, String fileIds)
    {
        return pickinglistService.save(pickinglist, fileIds);
    }

    @RepeatSubmit
    @Log(title = "审批", businessType = BusinessType.OTHER)
    @PostMapping("/approval")
    public AjaxResult approval(Long taskid,Long id,String opinion,@RequestParam(value="fileIds",required = false)String imgids){
        return pickinglistService.approval(id, imgids, opinion, taskid);
    }

    @RepeatSubmit
    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid,Long id,String opinion,String fileIds,Long refuseNodeid,Long backUserid){
        return pickinglistService.refuse(id, fileIds, opinion, taskid,refuseNodeid,backUserid);
    }

    /**作废
     领料出库及之前可以删除
     */
    @RepeatSubmit
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(Long id) {
        return pickinglistService.del(id);
    }

    /**
     * 入库申请whinapply、退料materapply、废品登记scrapapply，在领料单状态为30时，发起
     */

    /**
     * 获取领料单详细信息
     */
    @GetMapping(value = "getSpes")
    public AjaxResult getSpes(Long pickid, @RequestParam(required = false) String productid)
    {
        return pickinglistService.getSpes(pickid, productid);
    }

    /**
     * 退料入库获取产品
     */
    @GetMapping(value = "getProducts")
    public AjaxResult getProducts(Long pickinglistid)
    {
        return pickinglistService.getProducts(pickinglistid);
    }


    /**
     * 领辅料获取成本对象  工厂可生产品种
     */
    @GetMapping(value = "getCosproduct")
    public AjaxResult getCosproduct(String strguid, String productflag)
    {
        return pickinglistService.getCosproduct(strguid, productflag);
    }
}
